package com.general.framework.web.utils;

import cn.afterturn.easypoi.handler.inter.IExcelExportServer;
import com.general.framework.core.exception.BaseException;
import com.general.framework.core.exception.Ex;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Method;
import java.util.List;

@Slf4j
public class ExportBigExcelService implements IExcelExportServer {


    private Object interfaceObject;

    private String methodName;


    public ExportBigExcelService (Object interfaceObject, String methodName){
       this.interfaceObject = interfaceObject;
       this.methodName = methodName;
    }



    @Override
    public List<Object> selectListForExcelExport(Object queryParams, int page) {
        List<Object> objectList = null;
        try {
            Method pageMethod = queryParams.getClass().getMethod("setPage", Integer.class);
            pageMethod.invoke(queryParams, page);
            Method method = this.interfaceObject.getClass().getDeclaredMethod(this.methodName, queryParams.getClass());
            Object result = method.invoke(interfaceObject, queryParams);
            if(result != null){
                objectList =(List<Object>) result;
            }
        } catch (BaseException e){
            throw Ex.business(e.getMessage());
        } catch (Exception e){
            log.error("导出数据获取数据异常：", e);
            throw Ex.business("导出数据异常，请稍后再试！");
        }
        return objectList;
    }
}
